package com.hibernate.daoimp.friendlink;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.common.Constant;
import com.common.Page;
import com.hibernate.dao.friendlink.FriendLinkDao;
import com.hibernate.pojo.Articles;
import com.hibernate.pojo.FriendLink;
import com.struts.form.FriendLinkForm;

public class FriendLinkDaoImp extends HibernateDaoSupport implements FriendLinkDao{
	
	private boolean bool=false;
	public int record_count=0;
	/**
	 * save friend link
	 */
	public boolean saveFriendLink(FriendLink friendlink)
	{
		try
		{
			this.getHibernateTemplate().save(friendlink);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}

	/**
	 * query friend link list
	 */
	public Map findFriendLink(int page)
	{
		Map hashmap=new HashMap();
		record_count=page*Constant.PAGE_VIEW_COUNT;
		List list=this.getHibernateTemplate().find("select count(*) from FriendLink");
		Integer counts=(Integer)list.get(0);
		Page pageobject=new Page();	
		Map map=pageobject.pageCount(counts);
		map.put("pagecur", page);
		hashmap.put("page_content", map);
		list=getHibernateTemplate().executeFind(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			Query query;
			query = session.createQuery("from FriendLink order by ID desc");
			query.setFirstResult(record_count);
			query.setMaxResults(Constant.PAGE_VIEW_COUNT);
			List list = query.list();
			return list;
		}
		});
		hashmap.put("friendlink_list", list);
		return hashmap;
	}
	
	/**
	 * delete friend link
	 */
	public boolean deleteFriendLink(int id)
	{
		bool=false;
		try
		{
			FriendLink friendlink=(FriendLink)this.getHibernateTemplate().load(FriendLink.class, id);
			this.getHibernateTemplate().delete(friendlink);
			bool=true;
		}
		catch(Exception e)
		{
			bool=false;
			e.printStackTrace();
		}
		return bool;
	}
	
	/**
	 * audit friend link
	 */
	public boolean updateFriendLinkAudit(FriendLinkForm friendlinkform)
	{
		bool=false;
		try
		{
			FriendLink friendlink=(FriendLink)this.getHibernateTemplate().load(FriendLink.class, friendlinkform.getId());
			friendlink.setAudit(friendlinkform.getAudit());
			this.getHibernateTemplate().update(friendlink);
			bool=true;
		}
		catch(Exception e)
		{e.printStackTrace();}
		finally
		{
			return bool;
		}
	}
	
	/**
	 * delete batch message
	 */
	public boolean deleteBatchMessage(String ids)
	{
		bool=false;
		final String hql="delete from FriendLink where id in ("+ids+")";
		this.getHibernateTemplate().execute(new HibernateCallback(){
		public Object doInHibernate(Session session)throws HibernateException, SQLException
		{
			Query query = session.createQuery(hql);
			int n=query.executeUpdate();
			bool=true;
			return bool;
		}
		});
		return bool;
	}
}
